/*
 * 	Additional function for tables.html
 *	Written by ThemePixels	
 *	http://themepixels.com/
 *
 *	Copyright (c) 2012 ThemePixels (http://themepixels.com)
 *	
 *	Built for Amanda Premium Responsive Admin Template
 *  http://themeforest.net/category/site-templates/admin-templates
 */

jQuery(document).ready(function(){
	jQuery("#empBirth").datepicker({ 
		dateFormat: "yy-mm-dd",
		changeYear: true,
		changeMonth: true,
		beforeShow: function(i,e){
		   var z = jQuery(i).closest(".ui-dialog").css("z-index") + 4;
		   e.dpDiv.css('z-index', z);
		}});
	jQuery('#userTable').dataTable({
		"sPaginationType": "full_numbers",
		"oLanguage": {
			"sLengthMenu": "每页显示 _MENU_ 条记录",
			"sZeroRecords": "抱歉， 没有找到",
			"sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
			"sInfoEmpty": "没有数据",
			"sInfoFiltered": "(从 _MAX_ 条数据中检索)",
			"sSearch":"全文搜索:",
			"oPaginate": {
			"sFirst": "首页",
			"sPrevious": "前一页",
			"sNext": "后一页",
			"sLast": "尾页",
			},
			"sZeroRecords": "没有检索到数据",
			"sProcessing": "<img src='./loading.gif' />"
		}
	});
	
	
	jQuery('#addUserBtn').click(function(){
		jQuery('#user_dialog').attr("title","添加员工信息");
		jQuery('input[name="employee.empNo"]').parent().parent().hide();
		jQuery('#editUserInfo').attr("action","/employee/add");
		jQuery('#editBtn').hide();
		jQuery('#addBtn').show();
		jQuery("#user_dialog").dialog({
			 width:500,
		     modal:true
		});
	});
	
	jQuery("#deptId").change(function(){
		var deptId = jQuery("#deptId").val();
		jQuery.ajax({
			type: "post",
			url: "/post/postsByDept",
			dataType: "json",
			data: {deptId: deptId},
			success: function(data) {
				console.log(data);
				jQuery("#postId").html("");
				jQuery("#postId").append("<option value=''>--请选择--</option>");
				jQuery.each(data.posts,function(i, post){
					console.log(post);
					jQuery("#postId").append("<option value='"+post.postId +"'>"+ post.postName + "</option>");
				});
			}
		});
	});
	
	jQuery("#upload").uploadPreview({
		imgPreview: "#empPhoto",
		width: 120,
		height: 150,
		imgType: ["bmp", "gif", "png", "jpg"]
	});
});



function confirmDelete(id, no, name) {
	jConfirm('你确定要删除' + name + '吗?', '删除'+no+'员工', function(r) {
		if(r) {
			jQuery.ajax({
				   type: "post",
				   url: "/employee/delete",
				   dataType:"json",
				   data: {empId: id},
				   success: function(data){
					   if(data.flag == true) {
						   	window.location.reload();
						   	jQuery.jGrowl("恭喜您，删除员工成功！");
					   }else {
						   jQuery.jGrowl("很抱歉，删除员工失败！请先删除和员工相关的工资，奖金，考勤记录.");
					   }
				   },
					error:function(e) {  
						jQuery.jGrowl("很抱歉，删除员工失败！");
			        }
				 });
		}
	});
	return false;
};

function showEditDialog(id){
	jQuery.ajax({
		url:'/employee/employeeInfo',
		type:'POST',
		async:true,
		data:{
			id:id
		},
		dataType:'json',
		success:function(data){
			if(data.result.status == "success") {
				jQuery('#user_dialog').attr("title","修改员工信息");
				jQuery('input[name="employee.empNo"]').parent().parent().show();
				jQuery('#editUserInfo').attr("action","/employee/edit");
				jQuery('#editBtn').show();
				jQuery('#addBtn').hide();
				var posts = data.result.posts;
				jQuery("#postId").html("");
				jQuery("#postId").append("<option value=''>--请选择--</option>");
				jQuery.each(posts,function(i, post){
					jQuery("#postId").append("<option value='"+post.postId +"'>"+ post.postName + "</option>");
				});
				var employee = data.result.employee;
				jQuery('#employeeId').val(employee.empId);
				jQuery('#empNo').val(employee.empNo);
				jQuery('#empName').val(employee.empName);
				jQuery('#deptId').val(employee.deptId);
				jQuery('#postId').val(employee.postId);
				if(employee.empGender == "男") {
					jQuery('#boy').attr("checked","checked");
				} else {
					jQuery('#girl').attr("checked","checked");
				}
				jQuery('.empGender').val();
				jQuery('#empBirth').val(employee.empBirth);
				jQuery('#empPhone').val(employee.empPhone);
				jQuery('#empEmail').val(employee.empEmail);
				if(employee.empPhoto != '') {
					jQuery("#empPhoto").attr("src","/resources/employeePic/" + employee.empPhoto + "?V=" + new Date());
				}
				if(employee.isAdmin == 1) {
					jQuery('#isAdmin').attr("checked","checked");
				} else {
					jQuery('#notAdmin').attr("checked","checked");
				}
				jQuery('#empAddress').val(employee.empAddress);
				jQuery("#user_dialog").dialog({
					 width:500,
				});
			} else {
				alert("内部错误");
			}
		}
	});
};